function helper
{
echo '
Feature

RUN LIKE:
    sh catch_tpcc_perf_resource.sh ...

PARAMS:
    -p port   the port where openGauss running on, default 16666
    -s path   where to store res
    -d dbname database name
    -Q        catch query plan
    -S        catch pg_setting
    -V        catch gaussdb -V and pg_config
    -A        catch sysctl -a
    -N        catch nmon
    -T        catch top
    -P        catch perf top
    -F        catch fire flamegraph
    -C        catch cold flamegraph
'
}

rootpath=$(dirname $0)
pid=`date '+%Y-%m-%d-%H-%M'`
respath=$rootpath/tpcc_perf_resource_$pid
schdule_timer=0
port=16666
dbname='tpcc'

want_query_plan=false
want_pg_settings=false
want_gaussdb_version=false
want_sysctl_a=false
want_nmon=false
want_top=false
want_perf_top=false
want_fire_flamegraph=false
want_cold_flamegraph=false


while [ $# -gt 0 ]; do
    case "$1" in
        -p) port=$2; shift 1           ;;
        -s) respath=$2; shift 1        ;;
        -d) dbname=$2; shift 1         ;;
        -Q) want_query_plan=true       ;;
        -S) want_pg_settings=true      ;;
        -V) want_gaussdb_version=true  ;;
        -A) want_sysctl_a=true         ;;
        -N) want_nmon=true             ;;
        -T) want_top=true              ;;
        -P) want_perf_top=true         ;;
        -F) want_fire_flamegraph=true  ;;
        -C) want_cold_flamegraph=true  ;;
         *) helper; exit 1             ;;
    esac
    shift 1
done

function report_log
{
    log=`date`" [timer: $schdule_timer]: $1 "
    echo $log
    echo $log >> $respath/catch_tpcc_perf_resource.log
}


# sql plan -------------------------------------------------------------
function catch_query_plan
{
    if [ ! $want_query_plan ] || [ $schdule_timer -ne 0 ]; then
        return
    fi

    plancheck=$rootpath/../benchmark/planchecks/plancheck.sql
    gsql -p $port -d $dbname -f $plancheck > $respath/plancheck.out
    
    report_log  "finish to catch query plan"
}


# pg_settings -------------------------------------------------------------
function catch_pg_settings
{
    if [ ! $want_pg_settings ] || [ $schdule_timer -ne 0 ]; then
        return
    fi
    
    settingcheck=$rootpath/../benchmark/planchecks/settingcheck.sql
    gsql -p $port -d $dbname -f $settingcheck > $respath/pg_settings.out
    
    report_log  "finish to catch pg_settings"
}


# gaussdb -V && pg_config -------------------------------------------------
function catch_gaussdb_version
{
    if [ ! $want_gaussdb_version ] || [ $schdule_timer -ne 0 ]; then
        return
    fi

    gaussdb -V > $respath/gaussdb_version.out
    pg_config > $respath/pg_config.out
    
    report_log  "finish to catch gaussdb -V"
}


# sysctl -a -------------------------------------------------------------
function catch_sysctl_a
{
    if [ ! $want_sysctl_a ] || [ $schdule_timer -ne 0 ]; then
        return
    fi
    sysctl -a > $respath/sysctl_a.out
    report_log  "finish to catch sysctl -a"
}


# nmon -------------------------------------------------------------
function catch_nmon
{
    if [ ! $want_nmon ] || [ $schdule_timer -ne 0 ]; then
        return
    fi
    binpath=$rootpath/../tools/nmon/nmon_arm
    ./$binpath -f -c 360 -s 10 -m $respath
    report_log "start to catch nmon -f -c 360 -s 10 -m $respath"
}


# top -------------------------------------------------------------
function catch_top
{
    if [ ! $want_top ] || [ $schdule_timer -ne 0 ]; then
        return
    fi
    
    
    report_log "top not support now."
}


# perf top -------------------------------------------------------------
function catch_perf_top
{
    if [ ! $want_perf_top ] || [ $schdule_timer -ne 500 ]; then
        return
    fi
    
    
    report_log "perf top not support now."
}


# fire flamegraph -------------------------------------------------------------
fire_flamegraph=
function catch_fire_flamegraph
{
    if [ ! $want_fire_flamegraph ] || [ $schdule_timer -ne 600 ]; then
        return
    fi
    
    bin=$rootpath/../flamegraph/fire.sh
    sh $bin -o $respath
    
    report_log "finish to catch fire flamegraph."
}


# cold flamegraph -------------------------------------------------------------

function catch_cold_flamegraph
{
    if [ ! $want_cold_flamegraph ] || [ $schdule_timer -ne 700 ]; then
        return
    fi
    
    bin=$rootpath/../flamegraph/cold.sh
    sh $bin -o $respath

    report_log "finish to catch cold flamegraph."
}


# callback -------------------------------------------------------------
function catch_callback
{
    report_log "finish to catch nmon -f -c 360 -s 10 -m $respath"
    echo "catch tpcc perf resource finish"
}



for (( schdule_timer=0;schdule_timer<3600;schdule_timer=`expr $schdule_timer + 10`))
do
    catch_query_plan
    catch_pg_settings
    catch_gaussdb_version
    catch_sysctl_a
    catch_nmon
    catch_top
    catch_perf_top
    catch_fire_flamegraph
    catch_cold_flamegraph
    sleep 10
done
catch_callback
